package com.student.Activitys.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.student.Activitys.R;
import com.student.Activitys.activity.base.BaseActivity;
import com.student.Activitys.bean.LeaveParameterBean;
import com.student.Activitys.bean.ScheduleWeekBean;
import com.odd.Activitys.http.manager.ScheduleManager;
import com.student.Activitys.utils.Constant;
import com.student.Activitys.utils.DataUtils;
import com.student.Activitys.utils.ToastUtils;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 项目名称：智慧校园学生端
 * 类描述：请假 页面
 * 创建人：张飞祥
 * 创建时间：2017/9/28 17:10
 * 修改人：张飞祥
 * 修改时间：2017/9/28 17:10
 * 修改备注：
 */
public class LeaveActivity extends BaseActivity {

    @Bind(R.id.iv_return)
    ImageView ivReturn;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.et_leave_reason)
    EditText etLeaveReason;
    @Bind(R.id.tv_leave_course)
    TextView tvLeaveCourse;
    @Bind(R.id.tv_leave_time)
    TextView tvLeaveTime;
    @Bind(R.id.tv_leave_teacher)
    TextView tvLeaveTeacher;
    @Bind(R.id.btn_leave_submit)
    Button btnLeaveSubmit;
    @Bind(R.id.rg_leave_style)
    RadioGroup rgLeaveStyle;
    @Bind(R.id.rb_leave_matter)
    RadioButton rbLeaveMatter;
    @Bind(R.id.rb_leave_illness)
    RadioButton rbLeaveIllness;
    @Bind(R.id.rb_leave_other)
    RadioButton rbLeaveOther;

    //传递过来的实体
    ScheduleWeekBean bean = null;

    //教师名称
    public  String teacherName = "";
    //上课节次
    public  String skjc = "";
    //上课周次
    public  String skzc = "";
    //课程名
    public  String kcm = "";
    //请假内容
    public  String leaveReason = "";
    //请假类型
    public  String rbName = "";
    //初始化网络请求
    public ScheduleManager mApiManager;
    //弹窗 diaolog
    public MaterialDialog promptDialog;

    @Override
    protected void initUI() {
        setContentView(R.layout.act_leave);
        //设置标题
        tvTitle.setText("请假");
        //显示返回按钮
        ivReturn.setVisibility(View.VISIBLE);
        //网络请求接口 常驻方法
        mApiManager = mApiManager.getScheduleApiManager(LeaveActivity.this, LeaveActivity.this.getTaskId());
        // 获取传递过来的封了数据的Bundle
        Bundle bundle = this.getIntent().getExtras();
        // 获取 传递过来的 实体 数据
        bean = bundle.getParcelable(Constant.SCHEDULE_WEEK_BEAN);
        skzc = bundle.getString(Constant.SKZC); // 获取上课周次
        teacherName = bean.getTeacherName();    // 获取老师名字
        skjc = bean.getSKJC();                  // 获取上课节次
        kcm = bean.getKCM();                    // 获取课程名
    }

    @Override
    protected void initData() {
        /**
         * 获取页面数据源
         */
        //课程名
        tvLeaveCourse.setText(kcm);
        //时间 + 课节
        tvLeaveTime.setText(DataUtils.getNowDate("yyyy/MM/dd") + "（" + skjc.substring(0, 2) + "," + skjc.substring(2, 4) + " 节）");
        //老师
        tvLeaveTeacher.setText(teacherName);
        //请假类型
        rgLeaveStyle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rb_leave_matter:
                        rbName = rbLeaveMatter.getText().toString();
                        break;
                    case R.id.rb_leave_illness:
                        rbName = rbLeaveIllness.getText().toString();
                        break;
                    case R.id.rb_leave_other:
                        rbName = rbLeaveOther.getText().toString();
                        break;
                }
            }
        });

    }

    @Override
    protected void getNotData() {

    }

    /**
     * 日课表 请假 网络请求 加载是否成功
     */
    @Subscriber(tag = Constant.BUS_DAY_LEAVE_ON_COMPLETED,mode = ThreadMode.MAIN)
    public void initAddress(String str) {
        //销毁handler
        time_handler.removeCallbacks(time_runnable);
        timeDailog.dismiss();
        dialog.dismiss();
    }
    /**
     * 日课表 请假 网络请求 返回具体 数据实体
     */
    @Subscriber(tag = Constant.BUS_DAY_LEAVE_LIST,mode = ThreadMode.MAIN)
    public void initAddress(LeaveParameterBean leaveBean) {
        if (leaveBean.getState().equals("1")){
            promptDialog = new MaterialDialog.Builder(LeaveActivity.this)
                    .title("成功")
                    .content(leaveBean.getMsg())
                    .positiveText("确认")
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(MaterialDialog dialog, DialogAction which) {
                            //刷新日课表
                            EventBus.getDefault().post("refresh", Constant.SCHEDULE_DAY_REFRESH);
                            finish();
                        }
                    }).show();
        }else{
            promptDialog = new MaterialDialog.Builder(LeaveActivity.this)
                    .title("失败")
                    .content(leaveBean.getMsg())
                    .negativeText("取消")
                    .positiveText("重试")
                    .onNegative(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(MaterialDialog dialog, DialogAction which) {
                            promptDialog.dismiss();
                        }
                    })
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(MaterialDialog dialog, DialogAction which) {
                            setDateSubmit();
                        }
                    }).show();
        }
        // 设置点击屏幕Dialog不消失
        promptDialog.setCanceledOnTouchOutside(false);
        //取消Back键监听
        promptDialog.setCancelable(false);
    }

    /**
     * 所有点击事件
     *
     * @param view
     */
    @OnClick({R.id.iv_return, R.id.btn_leave_submit})
    void OnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_return:          //退出
                finish();
                break;
            case R.id.btn_leave_submit:   //提交
                setDateSubmit();
                break;
        }
    }

    /**
     * 数据提交
     */
    private void setDateSubmit() {
        //请假内容
        leaveReason = etLeaveReason.getText().toString();
        if (!leaveReason.equals("")){
            //请求日课表数据
            mApiManager.scheduleDayLeave(Constant.HTTP_DAY_LEAVE_ACTION, leaveReason,
                    bean.getJSH(),skjc,bean.getKCH(),skzc,DataUtils.getNowWeekNumber(),xh,
                    bean.getXNXQ(),rbName,DataUtils.getNowDate("yyyy-MM-dd"));
            //显示 加载dialog
            initShowTimeDialog("正在提交申请，请稍后...",LeaveActivity.this);
        }else{
            ToastUtils.showShort(LeaveActivity.this,"请填写请假理由~");
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        //刷新当前页面
        if (falg) {
            timeSecond = 10;
            setDateSubmit();
            falg = false;
        }
    }
}
